package dao;

import application.exceptions.ItemNotFoundException;
import mappings.ChemicalComposition;
import mappings.ChemicalElements;
import mappings.Material;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.classic.Session;
import org.hibernate.criterion.Restrictions;
import utils.HibernateUtil;

import java.util.List;

public class ChemicalCompositionDao
{
    private static SessionFactory sessionFactory = HibernateUtil.getSessionFactory();

    /*
     * Zwraca chemical_compositions dla zadanego material_id
     */
    public List<ChemicalComposition> getChemicalCompositionsForMaterial(int materialId)
    {
        Session session = sessionFactory.openSession();

        List<ChemicalComposition> chemicalCompositions = session.createCriteria(ChemicalComposition.class)
                .add(Restrictions.eq("material.id", materialId))
                .list();

        session.close();

        return chemicalCompositions;
    }

    public ChemicalComposition addChemicalComposition(int materialId, int chemicalElementId, double min, double max) throws ItemNotFoundException
    {
        ChemicalComposition chemicalComposition = new ChemicalComposition();

        org.hibernate.classic.Session session = sessionFactory.openSession();
        Transaction tx = session.beginTransaction();

        Material material = (Material) session.get(Material.class, materialId);
        ChemicalElements chemicalElement = (ChemicalElements) session.get(ChemicalElements.class, chemicalElementId);

        if(material == null || chemicalElement == null)
        {
            tx.rollback();
            session.close();
            throw new ItemNotFoundException();
        }

        chemicalComposition.setMaterial(material);
        chemicalComposition.setChemicalElements(chemicalElement);
        chemicalComposition.setMin(min);
        chemicalComposition.setMax(max);

        session.save(chemicalComposition);

        tx.commit();
        session.close();

        return chemicalComposition;
    }

    public ChemicalComposition editChemicalComposition(Integer id, int materialId, int chemicalElementId, double min, double max) throws ItemNotFoundException
    {
        org.hibernate.classic.Session session = sessionFactory.openSession();
        Transaction tx = session.beginTransaction();

        ChemicalComposition chemicalCompositionToEdit = (ChemicalComposition) session.get(ChemicalComposition.class, id);
        Material material = (Material) session.get(Material.class, materialId);
        ChemicalElements chemicalElement = (ChemicalElements) session.get(ChemicalElements.class, chemicalElementId);

        if(chemicalCompositionToEdit == null || material == null || chemicalElement == null)
        {
            tx.rollback();
            session.close();
            throw new ItemNotFoundException();
        }

        chemicalCompositionToEdit.setMaterial(material);
        chemicalCompositionToEdit.setChemicalElements(chemicalElement);
        chemicalCompositionToEdit.setMax(max);
        chemicalCompositionToEdit.setMin(min);
        session.update(chemicalCompositionToEdit);
        tx.commit();
        session.close();

        return chemicalCompositionToEdit;
    }

    public void deleteChemicalComposition(Integer id) throws ItemNotFoundException
    {
        org.hibernate.classic.Session session = sessionFactory.openSession();
        Transaction tx = session.beginTransaction();

        int affectedRows = session.createQuery("delete ChemicalComposition where id = :id")
                .setParameter("id", id)
                .executeUpdate();

        if(affectedRows == 0)
        {
            tx.rollback();
            session.close();
            throw new ItemNotFoundException();
        }

        tx.commit();
        session.close();
    }
}
